package com.raychi.demo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.raychi.demo.entity.Custom;
import com.raychi.demo.util.CustomUtil;

public class CustomDao {

	public List<Custom> getcustoms(Connection conn) throws ClassNotFoundException, SQLException{
		List<Custom> customs = new ArrayList<Custom>();
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery("select * from custom");
		Custom custom = null;
		while(rs.next()){
			custom = CustomUtil.createCustom(rs);
			customs.add(custom);
		}
		return customs;
	}
	
	public void delete(Custom custom, Connection conn) throws Exception{
		Statement st = conn.createStatement();
		String sql = "delete from custom where id="+custom.getId();
		st.execute(sql);
	}
	
	public void update(Custom custom, Connection conn) throws Exception{
		String sql = "update custom set name = ?, waittime = ? where id =" + custom.getId();
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, custom.getName());
		ps.setInt(2, custom.getWaittime());
		ps.executeUpdate();
	}
	
	public void save(Custom custom, Connection conn) throws Exception{
		String sql = "insert into custom (name, waittime) values (?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setString(1, custom.getName());
		ps.setInt(2, custom.getWaittime());
		ps.execute();
	}
}
